<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编码" prop="FNumber">
        <el-input v-model="queryParams.FNumber" placeholder="请输入编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="分析方法" prop="FAnalysisMethod">
          <el-input
            v-model="queryParams.FAnalysisMethod"
            placeholder="请输入分析方法"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="缺陷等级" prop="FDefectlevel">
          <el-input
            v-model="queryParams.FDefectlevel"
            placeholder="请输入缺陷等级"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
      <el-form-item label="重点检测" prop="FKeyInspect">
        <el-select v-model="queryParams.FKeyInspect" clearable>
          <el-option label="否" value="false"></el-option>
          <el-option label="是" value="true"></el-option>
        </el-select>

      </el-form-item>
      <!-- <el-form-item label="组别" prop="FGroup">
          <el-input
            v-model="queryParams.FGroup"
            placeholder="请输入组别"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="质量标准" prop="FQualityStdId">
          <el-input
            v-model="queryParams.FQualityStdId"
            placeholder="请输入质量标准"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="单位" prop="FUnitId">
          <el-input
            v-model="queryParams.FUnitId"
            placeholder="请输入单位"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
      <el-form-item label="名称" prop="FName">
        <el-input v-model="queryParams.FName" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mes:qc:project:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:inspectitem:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:inspectitem:remove']"
          >删除</el-button>
        </el-col> -->
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:inspectitem:export']"
          >导出</el-button>
        </el-col> -->
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleSet"
          v-hasPermi="['mes:qc:project:sync']">同步数据</el-button>
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="inspectitemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检验项目编码" align="center" prop="FNumber" />
      <el-table-column label="检验项目名称" align="center" prop="FName" />
      <!-- <el-table-column label="分析方法" align="center" prop="fanalysisMethod" />
        <el-table-column label="缺陷等级" align="center" prop="fdefectlevel" /> -->
      <el-table-column label="重点检测" align="center" prop="FKeyInspect">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.FKeyInspect" :active-value=true :inactive-value=false disabled></el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column label="组别" align="center" prop="fgroup" />
        <el-table-column label="质量标准" align="center" prop="fqualityStdId" />
        <el-table-column label="单位" align="center" prop="funitId" /> -->
      <!-- <el-table-column label="描述" align="center" prop="FDescription" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:qc:project:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mes:qc:project:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改检验项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="检验项目编码" prop="FNumber">
          <el-input v-model="form.FNumber" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="检验项目名称" prop="FName">
          <el-input v-model="form.FName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="重点检测" prop="FKeyInspect">
          <el-switch v-model="form.FKeyInspect" :active-value=true :inactive-value=false>
          </el-switch>
          <!-- <el-select v-model="form.FKeyInspect" >
              <el-option label="否" value="fales"></el-option>
              <el-option label="是" value="true"></el-option>
            </el-select> -->
        </el-form-item>
        <!-- <el-form-item label="描述" prop="fdescription">
            <el-input v-model="form.fdescription" placeholder="请输入描述" />
          </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInspectitem, getInspectitem, delInspectitem, addInspectitem, updateInspectitem, getJdyInspectutem } from "@/api/mes/qc/inspectitem";

export default {
  name: "Inspectitem",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检验项目表格数据
      inspectitemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        FID: null,
        FNumber: null,
        FAnalysisMethod: null,
        FDefectlevel: null,
        FKeyInspect: null,
        FGroup: null,
        FQualityStdId: null,
        FInspectMethodId: null,
        FInspectInstrumentId: null,
        FInspectBasisId: null,
        FSampleSchemeId: null,
        FUnitId: null,
        FName: null,
        FDescription: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fid: [
          { required: true, message: "金蝶云检验项目fid不能为空", trigger: "blur" }
        ],
        fgroup: [
          { required: true, message: "组别不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询检验项目列表 */
    getList() {
      this.loading = true;
      listInspectitem(this.queryParams).then(response => {
        response.rows.forEach(item => {
          item.FKeyInspect = item.FKeyInspect == "true" ? true : false
        })
        this.inspectitemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fid: null,
        fnumber: null,
        fanalysisMethod: null,
        fdefectlevel: null,
        fkeyInspect: null,
        fgroup: null,
        fqualityStdId: null,
        finspectMethodId: null,
        finspectInstrumentId: null,
        finspectBasisId: null,
        fsampleSchemeId: null,
        funitId: null,
        fname: null,
        fdescription: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检验项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row, '修改的row');
      this.reset();
      const id = row.id || this.ids
      getInspectitem(id).then(response => {
        response.data.FKeyInspect = response.data.FKeyInspect == "true" ? true : false
        this.form = response.data;
        console.log(this.form, 'res,form');
        this.open = true;
        this.title = "修改检验项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInspectitem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspectitem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const FNumber = row.FNumber
      this.$modal.confirm('是否确认删除检验项目编码为"' + FNumber + '"的数据项？').then(function () {
        return delInspectitem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/mes/qc/inspectitem/export', {
        ...this.queryParams
      }, `inspectitem_${new Date().getTime()}.xlsx`)
    },
    async handleSet() {
      try {
        const response = await getJdyInspectutem();
        console.info("response.data", response.data);
        if (response.code === 200) {
          this.$message({
            message: "数据同步成功",
            type: "success"
          });
        } else {
          throw new Error("数据同步失败");
        }
      } catch (err) {
        this.$message.error(err.message);
      } finally {
        this.loading = false;
      }
    }
  },

};
</script>